Ext.define('Service.mainPage.controller.MainPageController', {
	extend : 'WebOs.ex.DefaultController',

	views : [ 'Service.mainPage.view.InitContainer',
			'Service.mainPage.view.MapContainer',
			'Service.mainPage.view.ComAddWindow' ],

	stores : [ 'Service.mainPage.store.HtComStore',
	           'Service.mainPage.store.ComDetailStore'
	],

	models : [ 'Service.mainPage.model.HtComModel',
	           'Service.mainPage.model.ComDetailModel'

	],

	refs : [
	// getter setter
	],
	isAdding : false,

	init : function() {
		var me = this;
		this.control({
			'container[id=Service_mainPage_view_InitContainer]' : {
				afterrender : me.onMapContainerRenderFire
			},
			'button[id=Service_mainPage_view_QuitAddBtn]' : {
				click : me.onQuitAddBtnClickFire
			},
			'button[id=Service_mainPage_view_ComAddBtn]' : {
				click : me.onComAddBtnClickFire
			},
			'button[id=Service_mainPage_view_ComSaveBtn]' : {
				click : me.onSaveBtnClickFire
			},
			'button[id=Service_mainPage_view_ComCancelBtn]' : {
				click : me.onCancelBtnClickFire
			},
			'dataview[id=Service_mainPage_view_MapContainer]' : {
				afterrender : me.onDataviewRenderFire,
				itemmouseenter : me.onViewItemMouseEnterFire,
				itemmouseleave : me.onViewItemMouseLeaveFire,
				beforedestroy : me.beforeViewDestroyFire
			}

		});
	},
	
	tipMap : [],
	
	beforeViewDestroyFire : function(){
		var me = this;
		for (var p in me.tipMap){
			me.tipMap[p].destroy();
		}
	},
	
	onDetailStoreLoadFire : function(store){
		var me = this;
		store.each(function(record){
			var id = record.get('id');
			var container = Ext.create('Ext.container.Container',{
				floating : true,
				width : 200,
				height : 200,
				style : {
					backgroundColor : 'white'
				},
				id : 'com_stat_tip_'+id,
				padding : '10 10 10 10',
				html : '<div  style="width:100%;height:100%"><div style="margin:3px">小区名称 : '+record.get('name')+'</div><div style="margin:3px">住户数 : '+record.get('userCount')+'</div><div style="margin:3px">重点住户数 : '+record.get('impUserCount')+'</div><div style="margin:3px">重点区域数 : '+record.get('impPosCount')+'</div><div style="margin:3px">车辆数 : '+record.get('carCount')+'</div></div>'
			});
			me.tipMap.push(container);
		});
	},
	
	
	onViewItemMouseEnterFire : function(cmp,record,item,index,e){
		var me = this;
		var id = record.get('id');
		var tip = Ext.getCmp('com_stat_tip_'+id);
		item.style.cursor = "pointer";
		if(tip){
			tip.showAt(e.getPoint());
		}
	},
	
	onViewItemMouseLeaveFire : function(cmp,record,item,index,e){
		var me = this;
		var id = record.get('id');
		var tip = Ext.getCmp('com_stat_tip_'+id);
		if(tip){
			tip.hide();
		}
	},
	loadMapData : function(){
		var me = this;
		me.request({
			params : {
				'module' : 'Service_mainPage',
				'action' : 'getComDetails',
				'isPage' : 'false'
			},
			onSuccess : function(resp){
				var store = Ext.getCmp('Service_mainPage_view_MapContainer').getStore();
				store.loadData(resp.result);
				me.onDetailStoreLoadFire(store);
			}
		});
	},
	onDataviewRenderFire : function(cmp){
		this.loadMapData();
	},

	onMapContainerRenderFire : function(cmp) {
		var me = this;
		document.getElementById('map_img').onclick = function(e) {
			if (me.isAdding) {
				var addForm = Ext.create('Service.mainPage.view.ComAddWindow',
						{
							posX : e.offsetX + 7,
							posY : e.offsetY + 36
						});
				addForm.show();
			}
		};
		
	},
	onQuitAddBtnClickFire : function() {
		var me = this;
		var divDom = document.getElementById("map_addNote");
		if (divDom.style.visibility == "visible" && me.isAdding == true) {
			divDom.style.visibility = 'hidden';
			me.isAdding = false;
		}
	},

	onComAddBtnClickFire : function() {
		var me = this;
		var divDom = document.getElementById("map_addNote");
		if (divDom.style.visibility == "hidden" && me.isAdding == false) {
			divDom.style.visibility = 'visible';
			me.isAdding = true;
		}
	},

	onSaveBtnClickFire : function(cmp) {
		var me = this;
		var form = cmp.up('window').down('form[uploadType=normal]').getForm();
		var imgForm = cmp.up('window').down('form[uploadType=img]').getForm();
		if (imgForm.isValid()) {
			imgForm.submit({
				clientValidation : true,
				url : "imgUpload",
				waitMsg : '图片上传中',
				success : function(cm, resp) {
					if (form.isValid()) {
						var result = Ext.decode(resp.response.responseText);
						if (result['success'] == "true") {
							var param = form.getValues();
							var win = cmp.up('window');
							Ext.apply(param, {
								img : result['result'],
								module : 'Service_mainPage',
								action : 'addHtCom',
								posX : win.posX,
								posY : win.posY
							});
							
							Ext.Ajax.request({
								url : Constants.REQUEST_URL,
								method : 'POST',
								params : param,
								success : function(fp, o) {
									Ext.Msg.alert('成功', '保存成功');
									cmp.up('window').destroy();
									me.loadMapData();
								}
							});
						} else {
							alert("错误的文件类型,只支持png,jpg,jpeg,gif格式")
						}
					}
				}
			});
		}

	},

	onCancelBtnClickFire : function(cmp) {
		cmp.up('window').destroy();
	}

});
